package com.my.algorithmlearning.leetcode;

import org.springframework.util.Assert;

/**
 * @ClassName ClimbStairs
 * @Description ClimbStairs
 * @Author wanghaiwei
 * @Date 2022/11/22 15:31
 */
public class ClimbStairs {
    public int climbStairs(int n) {
        if (n <= 0) {
            return 0;
        } else if (n == 1) {
            return 1;
        } else if (n == 2) {
            return 2;
        } else {
            int[] memo = new int[n + 1];
            for (int i = 0; i < memo.length; i++) {
                memo[i] = -1;
            }
            memo[0] = 0;
            memo[1] = 1;
            memo[2] = 2;
            return climbStairsWithMemo(n, memo);
        }
    }

    private int climbStairsWithMemo(int n, int[] memo) {
        if (memo[n] != -1) {
            return memo[n];
        }
        memo[n] = climbStairsWithMemo(n - 1, memo) + climbStairsWithMemo(n - 2, memo);
        return memo[n];
    }

    public int climbStairsWithDynamicPrograming(int n) {
        if (n <= 0) {
            return 0;
        } else if (n == 1) {
            return 1;
        } else if (n == 2) {
            return 2;
        } else {
            int[] memo = new int[n + 1];
            for (int i = 0; i < memo.length; i++) {
                memo[i] = -1;
            }
            memo[0] = 0;
            memo[1] = 1;
            memo[2] = 2;
            for (int i = 3; i < memo.length; i++) {
                memo[i] = memo[i - 1] + memo[i - 2];
            }
            return memo[n];
        }
    }

    public int climbStairsWithDynamicProgramingMiniSpace(int n) {
        if (n <= 0) {
            return 0;
        } else if (n == 1) {
            return 1;
        } else if (n == 2) {
            return 2;
        } else {
            int[] memo = new int[3];
            memo[0] = 1;
            memo[1] = 2;
            for (int i = 2; i < n; i++) {
                memo[2] = memo[0] + memo[1];
                memo[0] = memo[1];
                memo[1] = memo[2];
            }
            return memo[2];
        }
    }

    public static void main(String[] args) {
        ClimbStairs solution = new ClimbStairs();
        Assert.isTrue(3 == solution.climbStairs(3), "备忘录递归爬3楼结果错误");
        Assert.isTrue(5 == solution.climbStairs(4), "备忘录递归爬4楼结果错误");
        Assert.isTrue(1836311903 == solution.climbStairs(45), "备忘录递归计算爬4楼结果错误");

        Assert.isTrue(3 == solution.climbStairsWithDynamicPrograming(3), "动态规划计算爬3楼结果错误");
        Assert.isTrue(5 == solution.climbStairsWithDynamicPrograming(4), "动态规划计算爬4楼结果错误");
        Assert.isTrue(1836311903 == solution.climbStairsWithDynamicPrograming(45), "动态规划计算爬4楼结果错误");

        Assert.isTrue(3 == solution.climbStairsWithDynamicProgramingMiniSpace(3), "动态规划+滑动数组计算爬3楼结果错误");
        Assert.isTrue(5 == solution.climbStairsWithDynamicProgramingMiniSpace(4), "动态规划+滑动数组计算爬4楼结果错误");
        Assert.isTrue(1836311903 == solution.climbStairsWithDynamicProgramingMiniSpace(45), "动态规划+滑动数组计算爬4楼结果错误");
    }
}
